﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using BluSteamClient.Model;
using System.Windows.Input;
using BluSteamClient.Model.Utility;
using BluSteamClient.Model.CommandImplementation;

namespace BluSteamClient.ViewModel
{
    public class LoginViewModel : INotifyPropertyChanged
    {
        public LoginViewModel()
        {
            // Connect to server
            TcpNetwork.Connect("127.0.0.1", 9000);

            // setup packet receiver 
            PacketReceiver.Instance.LoginSuccess += this.OnLoginSuccess;
            PacketReceiver.Instance.RegisterSuccess += this.OnRegisterSuccess;
            PacketReceiver.Instance.Start();

            this.Username = "Drewan";
            this.Password = "password";
        }

        #region Fields

        // commands
        private RelayCommand _loginCommand;
        private RelayCommand _registerCommand;

        private string _proceed;
        
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region Properties

        public string Username
        {
            get;
            set;
        }

        public string Password
        {
            get;
            set;
        }

        public string Email
        {
            get;
            set;
        }

        public string Proceed
        {
            get { return _proceed; }
            set
            {
                if (_proceed != value)
                {
                    _proceed = value;
                    OnPropertyChanged("Proceed");
                }
            }
        }

        #endregion

        #region Commands

        public ICommand LoginCommand
        {
            get
            {
                if (_loginCommand == null)
                {
                    _loginCommand = new RelayCommand(ParamArrayAttribute => this.Login());
                }
                return _loginCommand;
            }
        }

        public ICommand RegisterCommand
        {
            get
            {
                if (_registerCommand == null)
                {
                    _registerCommand = new RelayCommand(ParamArrayAttribute => this.Register());
                }
                return _registerCommand;
            }
        }

        #endregion
        
        #region Methods

        private void Login()
        {
            this.SetSessionData();

            // send login request
            Command loginCommand = new LoginCommand();
            loginCommand.Execute();
        }

        private void Register()
        {
            this.SetSessionData();

            // send register request
            Command registerCommand = new RegisterCommand();
            registerCommand.Execute();
        }

        private void SetSessionData()
        {
            Session.Username = this.Username;
            Session.Password = this.Password;
            Session.Email = this.Email;
        }

        #endregion

        #region Event Handlers

        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public void OnLoginSuccess(object sender, EventArgs e)
        {
            this.Proceed = "Proceed";
        }

        public void OnRegisterSuccess(object sender, EventArgs e)
        {
            this.Proceed = "Proceed";
        }

        #endregion
    }
}
